import os
from fastapi import FastAPI
from blog import app as blog_app
from tortoise.contrib.fastapi import register_tortoise

app = FastAPI(
    title='Fastapi orm crud',
    description='使用Fastapi和tortoise orm实现简单的增删改查项目',
    version='1.0.0',
    docs_url='/docs',
    redoc_url='/redocs',
)

app.include_router(blog_app, prefix='/blog', tags=['博客应用'])


# 连接模型和fastapi
register_tortoise(
    app,
    db_url=os.environ.get('TEST_DB', 'sqlite://:memory:'),  # 测试时使用内存数据库
    # db_url='mysql://root:root@localhost:3306/test01',  # 需要手动创建数据库 create database test01;
    modules={'models': ['blog.models']},
    generate_schemas=True,  # 生成表结构,只会创建一次
    add_exception_handlers=True
)

if __name__ == '__main__':
    import uvicorn

    uvicorn.run('run:app', debug=True, reload=True)
